// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import './my-theme/index.css'
import EasyScroll from 'easyscroll'
import 'babel-polyfill'
Vue.use(EasyScroll)
Vue.config.productionTip = false

Vue.use(ElementUI)
/* eslint-disable no-new */

// 登录验证
router.beforeEach((to, from, next) => {
  let token = store.state.token || sessionStorage.getItem('token')
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (token) { // 当前的token是否存在
      next()
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
